package common.kdt.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;

import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import common.kdt.api.KdtApiClient;

/**
 * KDTAPI
 */
public class KDTApi {
	
	private static final String APP_ID = "4a58c9ffaf434f67e7"; // 这里换成你的app_id
	private static final String APP_SECRET = "5cf1e98af684479c7c13c04dd5b32c1b"; // 这里换成你的app_secret
	private static final String KDT_ID = "17301178"; // 店铺ID
	
	/**
	 * kdt.crm.fans.points.get 获取粉丝积分
	 */
	public static Integer getKDT_Grade_ByOpenId(String openId) throws Exception {
		Integer grade = 0;
		// 获取fans_id
		String fans_id = get_fans_id(openId);
		String method = "kdt.crm.fans.points.get ";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("fans_id", fans_id);
		KdtApiClient kdtApiClient;
		HttpResponse response;

		kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
		response = kdtApiClient.get(method, params);
		//System.out.println("Response Code : "+ response.getStatusLine().getStatusCode());
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(response.getEntity().getContent()));
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = bufferedReader.readLine()) != null) {
			result.append(line);
		}
		//System.out.println(result.toString());
		JSONObject jobj = JSONObject.fromObject(result.toString());
		if (jobj.containsKey("response")) {
			jobj = jobj.getJSONObject("response");
			grade = jobj.getInt("point");
		}
		return grade;
	}

	/**
	 * 获取fans_id
	 */
	public static String get_fans_id(String openId) throws Exception {
		String method = "kdt.users.weixin.follower.get";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("weixin_openid", openId);

		KdtApiClient kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
		
		HttpResponse response = kdtApiClient.post(method, params, null, null);

		//System.out.println("Response Code : "+ response.getStatusLine().getStatusCode());
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(response.getEntity().getContent()));
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = bufferedReader.readLine()) != null) {
			result.append(line);
		}
		String fans_id = null;
		//System.out.println(result.toString());
		JSONObject jobj = JSONObject.fromObject(result.toString());
		if (jobj.containsKey("response")) {
			jobj = jobj.getJSONObject("response");
			if (jobj.containsKey("user")) {
				jobj = jobj.getJSONObject("user");
				fans_id = jobj.getString("user_id");
			}
		}
		return fans_id;
	}


	/**
	 * kdt.crm.fans.points.payin.get 给客户加积分
	 */
	public static Boolean  add_KDT_Grade(String openId,String points,String reason) throws Exception {
		boolean success = false;
		// 获取fans_id
		String fans_id = get_fans_id(openId);
		String method = "kdt.crm.fans.points.payin.get";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("fans_id", fans_id);
		params.put("kdt_id", KDT_ID);
		params.put("points", points);
		params.put("reason", reason);
		KdtApiClient kdtApiClient;
		HttpResponse response;

		kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
		response = kdtApiClient.get(method, params);
		//System.out.println("Response Code : "+ response.getStatusLine().getStatusCode());
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(response.getEntity().getContent()));
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = bufferedReader.readLine()) != null) {
			result.append(line);
		}
		//System.out.println(result.toString());
		JSONObject jobj = JSONObject.fromObject(result.toString());
		if (jobj.containsKey("response")) {
			jobj = jobj.getJSONObject("response");
			success = jobj.getBoolean("is_success");
		}
		return success;
	}

	
	/**
	 * kdt.crm.fans.points.payout.get 消耗用户积分
	 */
	public static Boolean del_KDT_Grade(String openId,String points,String reason) throws Exception {
		boolean success = false;
		// 获取fans_id
		String fans_id = get_fans_id(openId);
		String method = "kdt.crm.fans.points.payout.get";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("fans_id", fans_id);
		params.put("kdt_id", KDT_ID);
		params.put("points", points);
		params.put("reason", reason);
		KdtApiClient kdtApiClient;
		HttpResponse response;

		kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
		response = kdtApiClient.get(method, params);
		//System.out.println("Response Code : "+ response.getStatusLine().getStatusCode());
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(response.getEntity().getContent()));
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = bufferedReader.readLine()) != null) {
			result.append(line);
		}
		//System.out.println(result.toString());
		JSONObject jobj = JSONObject.fromObject(result.toString());
		if (jobj.containsKey("response")) {
			jobj = jobj.getJSONObject("response");
			success = jobj.getBoolean("is_success");
		}
		return success;
	}

	/**
	 * kdt.ump.coupons.unfinished.all 获取所有未结束的优惠列表（包含未开始的、进行中的）
	 */
	public static String get_card_list() throws Exception {
		boolean success = false;
		// 获取fans_id
		//String fans_id = get_fans_id(openId);
		String method = "kdt.ump.coupons.unfinished.all";
		HashMap<String, String> params = new HashMap<String, String>();
		//params.put("fans_id", fans_id);
		//params.put("kdt_id", KDT_ID);
		//params.put("points", points);
		//params.put("reason", reason);
		KdtApiClient kdtApiClient;
		HttpResponse response;

		kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
		response = kdtApiClient.get(method, params);
		//System.out.println("Response Code : "+ response.getStatusLine().getStatusCode());
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(response.getEntity().getContent()));
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = bufferedReader.readLine()) != null) {
			result.append(line);
		}
		//System.out.println(result.toString());
		JSONObject jobj = JSONObject.fromObject(result.toString());
		//if (jobj.containsKey("response")) {
		//	jobj = jobj.getJSONObject("response");
		//	success = jobj.getBoolean("is_success");
		//}
		return jobj.toString();
	}
	
	/**
	 *kdt.ump.coupon.take 微信粉丝领取优惠券优惠码
	 */
	public static String send_card_To_member(String openId ,String coupon_group_id) throws Exception {
		String msg = "";
		// 获取fans_id
		String fans_id = get_fans_id(openId);
		String method = "kdt.ump.coupon.take";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("fans_id", fans_id);
		params.put("coupon_group_id", coupon_group_id);
		params.put("openId", openId);
		KdtApiClient kdtApiClient;
		HttpResponse response;

		kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
		response = kdtApiClient.get(method, params);
		//System.out.println("Response Code : "+ response.getStatusLine().getStatusCode());
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(response.getEntity().getContent()));
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = bufferedReader.readLine()) != null) {
			result.append(line);
		}
		//System.out.println(result.toString());
		JSONObject jobj = JSONObject.fromObject(result.toString());
		if (jobj.containsKey("response")) {
			jobj = jobj.getJSONObject("response");
			msg = "卡卷发放success";
		}else{
			msg = "卡卷发放失败";
		}
		return msg;
	}
	
	/**
	 * kdt.users.weixin.follower.tags.add 根据微信粉丝用户的 openid 或 user_id 绑定对应的标签
	 */
	public static String add_user_to_tags(String openId ,String tags) throws Exception {
		String msg = "";
		String method = "kdt.users.weixin.follower.tags.add";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("tags", tags);
		params.put("weixin_openid", openId);
		KdtApiClient kdtApiClient;
		HttpResponse response;

		kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
		response = kdtApiClient.get(method, params);
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(response.getEntity().getContent()));
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = bufferedReader.readLine()) != null) {
			result.append(line);
		}
		//System.out.println(result.toString());
		JSONObject jobj = JSONObject.fromObject(result.toString());
		if (jobj.containsKey("response")) {
			jobj = jobj.getJSONObject("response");
			msg = "绑定成功success";
		}else{
			msg = "绑定失败";
		}
		return msg;
	}
}
